export default {
    name: "MedeviceList",
    components: {},
    data() {
        return {
            height: '600',
            total: 0,
            pagesize: 5,
            currentPage: 1,
            searchForm: {
                deviceId: '',
                deviceName: '',
                deviceStatue: null,
                deviceType: '',
            },
            alldevicelistData: [],
            statueData: [
                {
                    name: '全部',
                    value: null
                }, {
                    name: '在线',
                    value: true
                }, {
                    name: '离线',
                    value: false
                }
            ],
            TypeData: [],
            multipleSelection: [],
            handleAddDeviceDialog: false,
            fileList: [],
            fileUrl:'',
            fileData:'',
            file: '',
            formLabelWidth:'120px',
            handleDeviceConfirationDialog:false,
            TypeData:[
                {
                    deviceModelName:"版本号"
                }
            ],
            form:{
                deviceName:"",
                deviceType:""
            },
            firmwareData:[]
        };
    },
    computed: {
        offset: function () {
            return (this.currentPage - 1) * this.pagesize;
        }
    },
    watch: {},
    created() {
        this.initAllDeviceList();
        this.selectType();
    },
    mounted() { },
    methods: {
        //获取设备过滤名
        selectType() {
            this.$http.get("/deviceTypeController/queryAllDeviceType")
                .then((res) => {
                    if (res.status == 200) {
                        this.TypeData = res.data;
                    }
                }).catch((err) => {
                    console.log(err);
            });
        },
        initAllDeviceList() {
            var listParamsObj = {
                limit: this.pagesize,
                offset: this.offset,
                deviceStatus: " ",
                deviceName: this.searchForm.deviceName,
                deviceId: this.searchForm.deviceId,
                deviceModelName: this.searchForm.deviceType,
            }
            this.$http.post('/deviceCommonInfoController/devicesPage',this.qs.stringify(listParamsObj))
                .then((res) => {
                    if (res.status == 200) {
                        if(res.data !=null && res.total != null){
                            this.alldevicelistData = res.data;
                            this.total = res.total;
                        } else if(res.data == null && res.data == null){
                            this.alldevicelistData = [];
                            this.total = 0;
                            this.$message.error(res.msg);
                        }
                    }
                    return false;
                }).catch((err) => {
                console.log(err);
            });
        },
        createFirmware(){

        },
        createDevice() {
              //创建一个实例
              var params = new FormData();
              params.append("file",this.fileData);
              console.log("文件",params.get('file'));
              this.$instance.post('/deviceImportInfoController/addDevices', params)
                 .then((res) => {
                    console.log("res",res)
                    if(res.status == 200){
                     this.$message({
                         type:'success',
                         message:"添加成功",
                     });
                     this.initAllDeviceList();
                     this.handleAddDeviceDialog = false;
                     this.fileList = [];
                     return;
                    } else if(res.status == 500){
                        this.$message.error(res.msg);
                        return false;
                    }
                }).catch((err) => {
                console.log(err);
            })
        },
        //设备搜索
        searchAllDevice() {
            this.currentPage = 1;
            this.initAllDeviceList();
        },
        //添加设备
        addDeviceClick() {
            this.handleAddDeviceDialog = true;
        },
        //设备配备
        setClick() {
            if (this.multipleSelection.length == 0) {
                this.$message.error("至少选择一个设备信息");
                return false;
            } else if (this.multipleSelection.length > 1) {
                this.$message.error("只能选择一行数据进行操作");
                return false;
            } else {
                var postData = [];
                var deviceIdData = [];
                var registeredData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.rowKey);
                    deviceIdData.push(val.deviceId);
                    registeredData.push(val.registered);
                });
                var rowKey = postData.toString();
                var deviceId = deviceIdData.toString();
                var registered = registeredData.toString();
                console.log(registered);
                if(registered == "离线"){
                    this.$message.error("设备已经离线!");
                    return false;
                } else if(registered == "在线"){
                    this.$router.push({
                        name: "Deviceset",
                        params: {
                            keyId: rowKey,
                            deviceId:deviceId,
                        }
                  });
               }
            }
        },
        rowClick(row) {
            console.log(row);
            this.$router.push({
                name: "Devicedetail",
                params: {
                    rowId: row.rowKey,
                    row: row,
                }
            });
        },
        browseTest() {
            this.handleChangeFile();
        },
        handleChangeFile(file, fileList) {
            if (fileList == undefined) {
                return false;
            } else {
                var existFile = fileList.slice(0, this.fileList.length - 1).find(f => f.name === file.name)
                if (existFile) {
                    this.$message.error("当前文件已经存在");
                    this.fileList.pop();
                }
                this.fileList = fileList;
            }
        },
        //删除自定义方法
        addAttachment(file) {
            // 用于显示图片
            this.fileUrl = URL.createObjectURL(file.file);
            console.log(this.fileUrl);
            //保存一份文件信息，用于上传
            this.fileData = file.file;
            console.log(this.fileData);
        },
        handleSuccessfile(res, file, fileList) {
            this.$message({
                type: 'success',
                message: '文件上传成功'
            })
        },
        handleErrorFile(res, file, fileList) {
            this.$message.warning("文件上传失败");
        },
        handleExceed(file, fileList) {
            console.log(file);
            console.log(fileList);
            this.$message.warning('只能选择1个文件!');
        },
        handlePreview(file) {
            console.log(file);
        },
        handleRemoveFile(file, fileList) {
            console.log(file);
            console.log(fileList);
            this.fileList = fileList
        },
        browseTest() {
            this.$refs.upload.submit();
        },
        beforeUpload(file) {
            console.log(file);
            const extension1 = file.name.split('.')[1] === 'xlsx';
            const isLt5M = file.size / 1024 / 1024 < 10;
            if (!extension1) {
                this.$message.error("上传模板只能是.json格式的文件");
                return false;
            }
            if (!isLt5M) {
                this.$message.error("上传模板")
                return false;
            }
            return true;
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleSizeChange(val) {
            this.pagesize = val;
            this.initAllDeviceList();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.initAllDeviceList();
        }
    }
};